<!-- HTML header for doxygen 1.8.3.1-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.3.1"/>
<title>AngelScript: Compiling scripts</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="test/javascript" src="touch.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="aslogo_small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.3.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('doc_compile_script.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Macros</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Pages</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Compiling scripts </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>After <a class="el" href="doc_register_api.html">registering the application interface</a> it's time to compile the scripts that will be executed.</p>
<h1><a class="anchor" id="doc_compile_script_msg"></a>
Message callback</h1>
<p>Before starting the compilation, remember to have the message callback set in the engine so that you can get more information on compilation errors than just an error code. In fact, it is recommended to set the message callback right after creating the script engine, as the message callback may even be helpful while registering the application interface.</p>
<p>The message callback has been designed so that it doesn't output anything if there are no errors or warnings, so when everything is ok, you shouldn't get anything from it. But if the <a class="el" href="classas_i_script_module.html#a8acf107194c5f079d7f7507309ebe613">Build</a> method returns an error, the message callback will have received detailed information about the error.</p>
<p>If desired, the application may send its own messages to the callback via the <a class="el" href="classas_i_script_engine.html#a936ce6566af958bb75ba1c0945d8b03a">WriteMessage</a> method on the engine.</p>
<div class="fragment"><div class="line"><span class="comment">// Implement a simple message callback function</span></div>
<div class="line"><span class="keywordtype">void</span> MessageCallback(<span class="keyword">const</span> <a class="code" href="structas_s_message_info.html" title="Represents a compiler message.">asSMessageInfo</a> *msg, <span class="keywordtype">void</span> *param)</div>
<div class="line">{</div>
<div class="line">  <span class="keyword">const</span> <span class="keywordtype">char</span> *type = <span class="stringliteral">&quot;ERR &quot;</span>;</div>
<div class="line">  <span class="keywordflow">if</span>( msg-&gt;<a class="code" href="structas_s_message_info.html#a6aa9231534b8aea2a3099cdc3206bcc8" title="The type of message.">type</a> == <a class="code" href="angelscript_8h.html#a8badcd23652646db5c5c6981dc73d4f5a210c2023d6971d688a0302096acf945d" title="The message is a warning.">asMSGTYPE_WARNING</a> ) </div>
<div class="line">    type = <span class="stringliteral">&quot;WARN&quot;</span>;</div>
<div class="line">  <span class="keywordflow">else</span> <span class="keywordflow">if</span>( msg-&gt;<a class="code" href="structas_s_message_info.html#a6aa9231534b8aea2a3099cdc3206bcc8" title="The type of message.">type</a> == <a class="code" href="angelscript_8h.html#a8badcd23652646db5c5c6981dc73d4f5ae29dba474231c07149dca09a9258f80d" title="The message is informational only.">asMSGTYPE_INFORMATION</a> ) </div>
<div class="line">    type = <span class="stringliteral">&quot;INFO&quot;</span>;</div>
<div class="line">  printf(<span class="stringliteral">&quot;%s (%d, %d) : %s : %s\n&quot;</span>, msg-&gt;<a class="code" href="structas_s_message_info.html#aeca6368be12c84b62ed8c659b1e4615c" title="The script section where the message is raised.">section</a>, msg-&gt;<a class="code" href="structas_s_message_info.html#a21ef80321436f229a547411a6598ea21" title="The row number.">row</a>, msg-&gt;<a class="code" href="structas_s_message_info.html#a08b23a360ac52110323bbf4aad553d9d" title="The column.">col</a>, type, msg-&gt;<a class="code" href="structas_s_message_info.html#af76694c6342dd82ef6aca0dff42072f5" title="The message text.">message</a>);</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="comment">// Set the message callback when creating the engine</span></div>
<div class="line"><a class="code" href="classas_i_script_engine.html" title="The engine interface.">asIScriptEngine</a> *engine = <a class="code" href="angelscript_8h.html#acb6a62345d9cca6c9b5a3dac67d80d0b" title="Creates the script engine.">asCreateScriptEngine</a>();</div>
<div class="line">engine-&gt;<a class="code" href="classas_i_script_engine.html#a74192fe950808eb72a64e3e371f0ea02" title="Sets a message callback that will receive compiler messages.">SetMessageCallback</a>(<a class="code" href="angelscript_8h.html#a78f8f2c7f1c88b12e74a5ac47b4184ae" title="Returns an asSFuncPtr representing the function specified by the name.">asFUNCTION</a>(MessageCallback), 0, <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4a68ae43cc91cdfc3fa4590c9e6164e4f4" title="A cdecl function.">asCALL_CDECL</a>);</div>
</div><!-- fragment --><h1><a class="anchor" id="doc_compile_script_load"></a>
Loading and compiling scripts</h1>
<p>To build a script module you first obtain a module from the engine, then add the script sections, and finally compile them. A compiled script module may be composed of one or more script sections, so your application may store each section in different files, or even generate them dynamically. It doesn't matter in which order the script sections are added to the module, as the compiler is able to resolve all names regardless of where they are declared in the script.</p>
<div class="fragment"><div class="line"><span class="comment">// Create a new script module</span></div>
<div class="line"><a class="code" href="classas_i_script_module.html" title="The interface to the script modules.">asIScriptModule</a> *mod = engine-&gt;<a class="code" href="classas_i_script_engine.html#a9f7cdc52b59034e6e55eb8a56b427aa4" title="Return an interface pointer to the module.">GetModule</a>(<span class="stringliteral">&quot;module&quot;</span>, <a class="code" href="angelscript_8h.html#ae4cf50de5273eb8c03c6e91e6e014f0ca0843ab784ed9a9ea6cb47d915825186f" title="Always create a new module, discarding the existing one.">asGM_ALWAYS_CREATE</a>);</div>
<div class="line"></div>
<div class="line"><span class="comment">// Load and add the script sections to the module</span></div>
<div class="line"><span class="keywordtype">string</span> script;</div>
<div class="line">LoadScriptFile(<span class="stringliteral">&quot;script.as&quot;</span>, script);</div>
<div class="line">mod-&gt;<a class="code" href="classas_i_script_module.html#a330835919b565c76c25e9425536f50b7" title="Add a script section for the next build.">AddScriptSection</a>(<span class="stringliteral">&quot;script.as&quot;</span>, script.c_str());</div>
<div class="line"></div>
<div class="line"><span class="comment">// Build the module</span></div>
<div class="line"><span class="keywordtype">int</span> r = mod-&gt;<a class="code" href="classas_i_script_module.html#a8acf107194c5f079d7f7507309ebe613" title="Build the previously added script sections.">Build</a>();</div>
<div class="line"><span class="keywordflow">if</span>( r &lt; 0 )</div>
<div class="line">{</div>
<div class="line">  <span class="comment">// The build failed. The message stream will have received  </span></div>
<div class="line">  <span class="comment">// compiler errors that shows what needs to be fixed</span></div>
<div class="line">}</div>
</div><!-- fragment --><p>AngelScript doesn't provide built-in functions for loading script files as most applications have their own way of loading files. However, it is quite easy to write your own routines for loading script files, for example:</p>
<div class="fragment"><div class="line"><span class="comment">// Load the entire script file into a string buffer</span></div>
<div class="line"><span class="keywordtype">void</span> LoadScriptFile(<span class="keyword">const</span> <span class="keywordtype">char</span> *fileName, <span class="keywordtype">string</span> &amp;script)</div>
<div class="line">{</div>
<div class="line">  <span class="comment">// Open the file in binary mode</span></div>
<div class="line">  FILE *f = fopen(<span class="stringliteral">&quot;test.as&quot;</span>, <span class="stringliteral">&quot;rb&quot;</span>);</div>
<div class="line">  </div>
<div class="line">  <span class="comment">// Determine the size of the file</span></div>
<div class="line">  fseek(f, 0, SEEK_END);</div>
<div class="line">  <span class="keywordtype">int</span> len = ftell(f);</div>
<div class="line">  fseek(f, 0, SEEK_SET);</div>
<div class="line">  </div>
<div class="line">  <span class="comment">// Load the entire file in one call</span></div>
<div class="line">  script.resize(len);</div>
<div class="line">  fread(&amp;script[0], len, 1, f);</div>
<div class="line">  </div>
<div class="line">  fclose(f);</div>
<div class="line">} </div>
</div><!-- fragment --><p>As AngelScript doesn't load the files itself, it also doesn't have built-in support for including other files from within the script. However, if you look in the add-on directory, you'll find a <a class="el" href="doc_addon_build.html">CScriptBuilder</a> class that provides this support and more. It is a helper class for loading files, perform a pre-processing pass, and then building the module. You can a see an example of how to use the script builder in <a class="el" href="doc_hello_world.html">Your first script</a>.</p>
<dl class="section see"><dt>See Also</dt><dd><a class="el" href="doc_adv_precompile.html">Pre-compiled byte code</a> </dd></dl>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Sun Dec 18 2016 12:35:28 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.3.1 </li>
  </ul>
</div>
</body>
</html>
